package com.rh.service.Impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.rh.exception.EducationException;
import com.rh.service.VodService;
import com.rh.utils.ConstantVodProperties;
import com.rh.utils.InitVodClient;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Service
public class VodServiceImpl implements VodService {
    /**
     * 上传视频到阿里云
     */
    @Override
    public String uploadVideoAly(MultipartFile file) {
        try {
            //UploadVideoRequest参数：title-上传后显示的名称 fileName-上传文件原始名称 inputStream-上传文件流
            //accessKeyId
            String accessKeyId = ConstantVodProperties.KEY_ID;
            //accessKeySecret
            String accessKeySecret = ConstantVodProperties.KEY_SECRET;
            //fileName
            String fileName = file.getOriginalFilename();
            //title,截取文件名称
            String title = fileName.substring(0, fileName.lastIndexOf("."));
            //inputStream
            InputStream inputStream = file.getInputStream();
            UploadStreamRequest request = new UploadStreamRequest(accessKeyId, accessKeySecret, title, fileName, inputStream);
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);
            System.out.print("RequestId=" + response.getRequestId() + "\n");  //请求视频点播服务的请求ID
            //设置视频id
            String videoId = null;
            if (response.isSuccess()) {
                videoId = response.getVideoId();
                System.out.print("VideoId=" + response.getVideoId() + "\n");
            } else {
                videoId = response.getVideoId();
                System.out.print("VideoId=" + response.getVideoId() + "\n");
                System.out.print("ErrorCode=" + response.getCode() + "\n");
                System.out.print("ErrorMessage=" + response.getMessage() + "\n");
            }
            return videoId;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 删除阿里云视频
     * @param id
     */
    @Override
    public void removeVideo(String id) {
        try {
            //初始化
            DefaultAcsClient client = InitVodClient.initVodClient(ConstantVodProperties.KEY_ID, ConstantVodProperties.KEY_SECRET);
            //创建删除视频的request对象
            DeleteVideoRequest request=new DeleteVideoRequest();
            //向request中设置视频id
            request.setVideoIds(id);
            client.getAcsResponse(request);
        } catch (Exception e) {
            e.printStackTrace();
            throw new EducationException(20001,"删除视频失败!");
        }
    }

    /**
     * 删除多个阿里云视频
     * @param videoIdList
     */
    @Override
    public void removeManyAlyVideo(List videoIdList) {
        try {
            //初始化
            DefaultAcsClient client = InitVodClient.initVodClient(ConstantVodProperties.KEY_ID, ConstantVodProperties.KEY_SECRET);
            //创建删除视频的request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            //向request中设置视频id
            //使用org.apache.commons.lang.StringUtils.join中的toArray方法设置ids
            String ids = StringUtils.join(videoIdList.toArray(), ",");
            request.setVideoIds(ids);
            client.getAcsResponse(request);
        } catch (Exception e) {
            e.printStackTrace();
            throw new EducationException(20001, "删除多个视频失败!");
        }
    }
    /**
     * 根据视频id获取播放凭证
     * @return
     */
    @Override
    public String getAuth(String id) {
        try{
            //创建初始化对象
            DefaultAcsClient client = InitVodClient.initVodClient(ConstantVodProperties.KEY_ID, ConstantVodProperties.KEY_SECRET);
            //创建获取视频地址request对象和response对象
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            //向request对象设置视频id值
            request.setVideoId(id);

            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            //获取视频播放凭证
            return response.getPlayAuth();
        }catch (Exception e){
            e.printStackTrace();
            throw new EducationException(20001,"获取播放凭证失败!");
        }
    }
    /**
     * 测试把集合中的数据改为用，号隔开
     * @param args
     */
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("11");
        list.add("22");
        list.add("33");
        //这里的StringUtils是org.apache.commons.lang.StringUtils;
        //toArray是变成数组,"," -用逗号隔开
        String join = StringUtils.join(list.toArray(), ",");
        System.out.println(join);
    }
}
